package com.songminju.sqlsong.orm;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Table;

public class JpaOrmManager implements OrmManager{

	public static final String Symbol = "@";
	
	class ClassInfo{
		@SuppressWarnings("rawtypes")
		Class c = null;
		String tableName = null;
		Map<String,String> fieldToColumn = new HashMap<>();
		Map<String,Field> columnToField = new HashMap<>();
		
		ClassInfo(@SuppressWarnings("rawtypes") Class c){
			this.c = c;
			tableName = c.getSimpleName();
			@SuppressWarnings("unchecked")
			Table tableAnno = (Table) c.getAnnotation(Table.class);
			if(tableAnno != null) {
				tableName = tableAnno.name();
			}
			Field[] fields = c.getDeclaredFields();
			for(Field f:fields) {
				Column colu = f.getAnnotation(Column.class);
				if(colu != null) {
					fieldToColumn.put(f.getName(), colu.name());
					columnToField.put(colu.name(), f);
				}
			}
		}
		
		String column(String field) {
			return fieldToColumn.get(field);
		}
		Field field(String column) {
			return columnToField.get(column);
		}
	}
	
	@SuppressWarnings("rawtypes")
	private Map<String,Class> classes = new HashMap<>();
	private Map<String,ClassInfo> cInfos = new HashMap<>();
	
	@Override
	public void registerEntity(@SuppressWarnings("rawtypes") Class c) {
		classes.put(c.getName(), c);
		cInfos.put(c.getName(),new ClassInfo(c));
	}
	
	@Override
	public String translateToSql(String ssql) {
		return ssql;
	}

}
